package com.lulalei.dbviewer.commons.utils;

import com.lulalei.dbviewer.commons.exception.CustomizeException;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ProjectPathUtils {

    public static String getOwnModulePackageDir() throws XmlPullParserException, IOException, CustomizeException {
        List<String> modules = PomReaderUtils.getModules(PomReaderUtils.getRootPomModel());
        if (modules.isEmpty()) {
            return getSingleModulePackageDir();
        } else {
            String artifactId = PomReaderUtils.getCurrentPomModel().getArtifactId();
            return getModulePackageDir(artifactId);
        }
    }


    public static String getOtherModulePackageDir(String moduleName) throws CustomizeException, XmlPullParserException, IOException {
        return getModulePackageDir(moduleName);
    }

    /**
     * 获取项目resources目录路径
     */
    public static String getOwnModuleResourcesDir() throws XmlPullParserException, IOException {
        List<String> modules = PomReaderUtils.getModules(PomReaderUtils.getRootPomModel());
        if (modules.isEmpty()) {
            return getSingleModuleResourcesDir();
        } else {
            String artifactId = PomReaderUtils.getCurrentPomModel().getArtifactId();
            return getModuleResourcesDir(artifactId);
        }


    }

    /**
     * 获取项目resources目录路径
     */
    public static String getOtherModuleResourcesDir(String moduleName) throws XmlPullParserException, IOException {
        return getModuleResourcesDir(moduleName);
    }

    /**
     * 获取项目java目录路径
     */
    private static String getSingleModuleJavaDir() {
        String path = getSingleModuleMainDir() + File.separator + "java";
        buildPath(path);
        return path + File.separator;
    }

    /**
     * 获取项目子模块的java目录路径
     */
    private static String getModuleJavaDir(String moduleName) {
        String path = getModuleMainDir(moduleName) + File.separator + "java";
        buildPath(path);
        return path + File.separator;
    }


    public static String buildPath(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }

    /**
     * 获取单个模块项目的resources目录路径
     */
    private static String getSingleModuleResourcesDir() {
        String path = getSingleModuleMainDir() + File.separator + "resources";
        buildPath(path);
        return path + File.separator;
    }

    /**
     * 获取多模块项目的resources目录路径
     */
    private static String getModuleResourcesDir(String moduleName) {
        String path = getModuleMainDir(moduleName) + File.separator + "resources";
        buildPath(path);
        return path + File.separator;
    }


    /**
     * 获取项目Main目录路径
     */
    private static String getSingleModuleMainDir() {
        //项目路径
        String projectPath = System.getProperty("user.dir");

        //包的全路径
        String[] srcPath = {"src", "main"};

        return projectPath + File.separator + String.join(File.separator, srcPath) + File.separator;
    }


    /**
     * 获取多个模块项目的包路径
     */
    private static String getModulePackageDir(String moduleName) throws XmlPullParserException, IOException, CustomizeException {
        //包相对于java目录的相对路径
        String rootGroupId = PomReaderUtils.getGroupId();
        String rootArtifactId = PomReaderUtils.getArtifactId(PomReaderUtils.getChildrenPomModel(moduleName));
        String[] split = rootGroupId.split("\\.");
        String[] artifactIds = rootArtifactId.split("-");
        //绝对路径
        String path = getModuleJavaDir(moduleName) + String.join(File.separator, split) + File.separator + String.join(File.separator, artifactIds);
        buildPath(path);
        return path + File.separator;
    }

    /**
     * 获取一个模块项目的包路径
     */
    private static String getSingleModulePackageDir() throws XmlPullParserException, IOException {
        //包相对于java目录的相对路径
        String rootGroupId = PomReaderUtils.getGroupId();
        String rootArtifactId = PomReaderUtils.getArtifactId(PomReaderUtils.getCurrentPomModel());
        String[] split = rootGroupId.split("\\.");
        String[] artifactIds = rootArtifactId.split("-");
        //绝对路径
        String path = getSingleModuleJavaDir() + String.join(File.separator, split) + File.separator + String.join(File.separator, artifactIds);
        buildPath(path);
        return path + File.separator;
    }

    /**
     * 获取项目子模块的Main目录路径
     */
    private static String getModuleMainDir(String moduleName) {
        //项目路径
        String projectPath = System.getProperty("user.dir");

        //包的全路径
        String[] srcPath = {"src", "main"};

        return projectPath + File.separator + moduleName + File.separator + String.join(File.separator, srcPath) + File.separator;
    }


}
